#include <iostream>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>

int gcnt = 100;

// 新线程
void *ThreadRoutine(void *arg)
{
    const char *threadname = (const char *)arg;
    while (true)
    {
        std::cout << "I am a new thread: " << threadname << ", pid: " << getpid() << "gcnt: " << gcnt << " &gcnt: " << &gcnt << std::endl;
        gcnt--;
        sleep(1);
    }
}

int main()
{
    // 已经有进程了
    pthread_t tid;
    pthread_create(&tid, NULL, ThreadRoutine, (void *)"thread 1");

    // sleep(3);

    // pthread_t tid1;
    // pthread_create(&tid1, nullptr, ThreadRoutine, (void *)"thread 2");


    // sleep(3);


    // pthread_t tid2;
    // pthread_create(&tid2, nullptr, ThreadRoutine, (void *)"thread 3");

    // sleep(3);

    // pthread_t tid3;
    // pthread_create(&tid3, nullptr, ThreadRoutine, (void *)"thread 4");

    // sleep(3);

    // 主线程
    while (true)
    {
        std::cout << "I am main thread"
                  << ", pid: " << getpid()  << "gcnt: " << gcnt << " &gcnt: " << &gcnt << std::endl;
        sleep(1);
    }
    return 0;
}